package com.gonsin.ta.logic.node;

import com.gonsin.ta.logic.enums.NodeTypeEnum;
import com.gonsin.ta.logic.node.groovy.GroovyNodeAdapter;
import com.gonsin.ta.logic.node.groovy.GroovyScriptExecutor;
import com.gonsin.ta.logic.process.TaBus;

public class TaNodeBuilder {


    /**
     * ta.xml文件的文件名称
     */
    private String filename;

    private String groovyScript;

    private String nodeId;

    private NodeTypeEnum nodeType;

    private Class nodeClass;

    /**
     * groovy 处理类
     */
    private GroovyScriptExecutor groovyScriptExecutor;

    public TaNodeBuilder(GroovyScriptExecutor groovyScriptExecutor) {
        this.groovyScriptExecutor = groovyScriptExecutor;
    }

    public static TaNodeBuilder createBuilder(GroovyScriptExecutor groovyScriptExecutor) {
        return new TaNodeBuilder(groovyScriptExecutor);
    }


    public TaNodeBuilder setFilename(String filename) {
        this.filename = filename;
        return this;
    }


    public TaNodeBuilder setGroovyScript(String groovyScript) {
        this.groovyScript = groovyScript;
        return this;
    }


    public TaNodeBuilder setNodeId(String nodeId) {
        this.nodeId = nodeId;
        return this;
    }

    public void build() {
        if(nodeClass == null){
            GroovyNodeAdapter node = new GroovyNodeAdapter(nodeId, nodeType, groovyScript, filename, groovyScriptExecutor);
            TaBus.addNode(node, true);
        } else {
            GroovyNodeAdapter node = new GroovyNodeAdapter(nodeId, nodeType, nodeClass, groovyScript, filename, groovyScriptExecutor);
            TaBus.addNode(node, true);
        }
    }

    public TaNodeBuilder setNodeType(NodeTypeEnum nodeType) {
        this.nodeType = nodeType;
        return this;
    }

    /**
     * 如果是事先编译过class的，则直接试用，此方法与  @setGroovyScript 不能同时用
     */
    public TaNodeBuilder setClass(Class clazz) {
        this.nodeClass = clazz;
        return this;
    }
}
